﻿/*
 ***************************************************************************
 * Microsoft Visual C# .NET
 * 名称：       App_Master_RoomInfo
 * 类型：       房间信息维护
 * 功能说明：   添加/更改/删除房间信息
 * 作者：       谈震鸣
 * 创建日期：   2007/08/16
 * 修改日期     修改者      修改内容
 * 
 * 
 * ---
 ***************************************************************************
 */
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Master.Dao;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.System;
using Shpm.App.Master.Bean;
using System.IO;

public partial class App_Master_RoomInfo : System.Web.UI.Page
{
    public string cmbRoomLst = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
    {   
        if (!Page.IsPostBack)
        {
            ApartmentDao dao = new ApartmentDao();
            IList dataList = dao.GetApartmentSchemeList();
            ddlScheme.DataSource = dataList;
            ddlScheme.DataBind();
            ddlScheme.Items.Insert(0, new ListItem());
            SetCmbRoom();
            ChangeDIV(cmbRoom.SelectedValue);
            CheckCmbValue();
            ViewState["CMBROOMLST"] = cmbRoomLst;
        }
        //Response.Write("<script language=JavaScript> changediv('" + cmbRoom.SelectedValue + "');</script>");
    }

    #region 检索按钮
    /*
     *名称： btnSerch_Click
     *功能： 左击检索按钮
     *参数： 无
     *返回： 无
     *说明： 无
     */
    protected void btnSerch_Click(object sender, EventArgs e)
    {
        AllClear();
        if (ComValidator1.DoValidator())
        {
            CheckImf(txtRoomIDSel.Text);
        }
    }
    #endregion

    #region 登录按钮
    /*
     *名称： btnAdd_Click
     *功能： 左击登录按钮
     *参数： 无
     *返回： 无
     *说明： 添加或更新房间信息
     */
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (ComValidator2.DoValidator())
        {
            MstApartmentBean dataBean = new MstApartmentBean();
            dataBean.ApartmentNo = txtApartmentNo.Text;
            dataBean.BuildingNo = txtBuildingNo.Text;
            if (txtFloor.Text != string.Empty && txtFloor.Text.Length == 1) txtFloor.Text = 0 + txtFloor.Text;
            dataBean.Floor=txtFloor.Text;
            dataBean.Location = txtRoomPosition.Text;
            dataBean.Memo = txtRoomNote.Text;
            dataBean.Name = txtRoomName.Text;
            dataBean.RoomNo = txtRoomNo.Text;
            if(txtCapacity.Text!=string.Empty) dataBean.Capacity = int.Parse(txtCapacity.Text);
            dataBean.Model = cmbRoom.SelectedValue;
            dataBean.Status = rblRoom.SelectedValue;
            dataBean.SchemeId = (string.IsNullOrEmpty(ddlScheme.SelectedValue))?null:new Nullable<int>(int.Parse(ddlScheme.SelectedValue));
            SysUserManager manager = new SysUserManager(Context);
            SysUserBean sysUserBean = manager.getSysUserBean();
            MstSysUserBean mstSysUserBean = sysUserBean.getMstSysUserBean();
            dataBean.UserId = mstSysUserBean.SysUserId;
            ApartmentDao Dao = new ApartmentDao();
            if (UpdateCheckImf(txtApartmentNo.Text))
            {
                Dao.UpdateApartment(dataBean);
                ((Design_MasterPage)this.Master).InfoMessage = 
                    HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0002").ToString();
            }
            else
            {
                Dao.InsertApartment(dataBean);
                ((Design_MasterPage)this.Master).InfoMessage = 
                    HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0001").ToString();
            }
            CheckImf(txtApartmentNo.Text);
        }
    }
    #endregion

    #region 删除按钮
    /*
     *名称： btnDel_Click
     *功能： 左击删除按钮
     *参数： 无
     *返回： 无
     *说明： 删除房间信息
     */
    protected void btnDel_Click(object sender, EventArgs e)
    {
        if (ComValidator3.DoValidator())
        {
            MstApartmentBean dataBean = new MstApartmentBean();
            dataBean.ApartmentNo = txtApartmentNo.Text;
            ApartmentDao Dao = new ApartmentDao();
            Dao.DeleteApartment(dataBean);
            HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0003").ToString();
            AllClear();
            ChangeDIV("");
        }
    }
    #endregion

    #region 清空信息
    /*
     *名称： AllClear
     *功能： 清空信息
     *参数： 无
     *返回： 无
     *说明： 无
     */
    private void AllClear()
    {
        txtApartmentNo.Text = string.Empty;
        txtBuildingNo.Text = string.Empty;
        txtFloor.Text=string.Empty ;
        txtRoomNo.Text = string.Empty;
        txtRoomName.Text = string.Empty;
        txtRoomNote.Text = string.Empty;
        txtRoomPosition.Text = string.Empty;
        txtCapacity.Text = string.Empty;
        rblRoom.SelectedIndex = 0;
        cmbRoom.SelectedIndex = -1;
    }
    #endregion

    #region 检查信息是否存在
    /*
     *名称： CheckImf
     *功能： 检查信息是否存在
     *参数： str
     *返回： 无
     *说明： 无
     */
    private void CheckImf( string str)
    {
        MstApartmentBean dataBean = new MstApartmentBean();
        dataBean.ApartmentNo = str;
        ApartmentDao Dao = new ApartmentDao();
        dataBean = Dao.Load(dataBean);
        if (dataBean != null)
        {
            SetRoomInf(dataBean);
        }
        else
        {
        }
    }
    #endregion

    #region 显示信息
    /*
     *名称： SetRoomInf
     *功能： 显示信息
     *参数： dataBean
     *返回： 无
     *说明： 无
     */
    private void SetRoomInf(MstApartmentBean dataBean)
    {
        txtApartmentNo.Text = dataBean.ApartmentNo;
        txtBuildingNo.Text = dataBean.BuildingNo;
        txtFloor.Text = dataBean.Floor;
        txtRoomNo.Text = dataBean.RoomNo;
        txtRoomName.Text = dataBean.Name;
        txtRoomNote.Text = dataBean.Memo;
        txtRoomPosition.Text = dataBean.Location;
        if(dataBean.Capacity>0) txtCapacity.Text = dataBean.Capacity.ToString();
        rblRoom.SelectedValue = dataBean.Status;
        cmbRoom.SelectedValue  = dataBean.Model;
        ddlScheme.SelectedValue = Convert.ToString(dataBean.SchemeId);
        ChangeDIV(cmbRoom.SelectedValue);
    }
    #endregion

    #region 判断更新或者插入
    /*
     *名称： UpdateCheckImf
     *功能： 判断更新或者插入
     *参数： str
     *返回： true(更新) false(插入)
     *说明： 无
     */
    private bool UpdateCheckImf(string str)
    {
        MstApartmentBean dataBean = new MstApartmentBean();
        dataBean.ApartmentNo = str;
        ApartmentDao Dao = new ApartmentDao();
        if (Dao.Load(dataBean) != null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    #endregion

    #region 取消按钮
    /*
     *名称： btnCancel_Click
     *功能： 左击取消按钮
     *参数： 无
     *返回： 无
     *说明： 清空信息
     */
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        AllClear();
    }
    #endregion



    #region 显示DIV
    /*
     *名称： ChangeDIV
     *功能： 显示DIV
     *参数： str
     *返回： 无
     *说明： 无
     */
    private void ChangeDIV(string str)
    {
        ((HtmlContainerControl)roomA).Style.Add("display", "");
        string strSrc = "../../Design/Room_" + str + ".htm";
        string fPath = Server.MapPath(strSrc);
        if (File.Exists(fPath))
            ((HtmlGenericControl)iRoom).Attributes.Add("src", strSrc);
        else
            ((HtmlContainerControl)roomA).Style.Add("display", "none");
    }
    #endregion

    protected void ddlScheme_SelectedIndexChanged(object sender, EventArgs e)
    {
        ChangeDIV(cmbRoom.SelectedValue);
    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        cmbRoomLst = Convert.ToString(ViewState["CMBROOMLST"]);
        if ( string.IsNullOrEmpty(ddlScheme.SelectedValue)==false)
        {
            ApartmentDao dao = new ApartmentDao();
            ApartmentSchemeDetailBean paramBean = new ApartmentSchemeDetailBean();
            paramBean.SchemeId = int.Parse(ddlScheme.SelectedValue);
            IList dataList = dao.FindByApartmentScheme(paramBean);
            GridView1.DataSource = dataList;
            GridView1.DataBind();
        }
    }
    private void SetCmbRoom()
    {
        CommonDictionaryBean paramBean = new CommonDictionaryBean();
        paramBean.Catalog = "ROOM_TYPE";
        CommonDictionaryDao dao = new CommonDictionaryDao();
        IList dataList = dao.FindByCatalog(paramBean);
        cmbRoom.DataSource = dataList;
        cmbRoom.DataBind();
        if (SetCmbIndex(dataList) != -1)
            cmbRoom.SelectedIndex = SetCmbIndex(dataList);
    }

    //设置下拉框的默认值
    private int SetCmbIndex(IList list)
    {
        CommonDictionaryBean dataBean = new CommonDictionaryBean();
        for (int i = 0; i < list.Count; i++)
        {
            dataBean = (CommonDictionaryBean)list[i];
            if (dataBean.Default == true) return i;
        }
        return -1;
    }

    private void CheckCmbValue()
    {
        string str=string.Empty;
        for (int i = 0; i < cmbRoom.Items.Count; i++)
        {
            str = cmbRoom.Items[i].Value.ToString();
            string strSrc = "../../Design/Room_" + str + ".htm";
            string fPath = Server.MapPath(strSrc);
            if (!File.Exists(fPath))
            {
                if (cmbRoomLst.Length > 0)
                {
                    cmbRoomLst = cmbRoomLst + ",\"" + str + "\"";
                }
                else
                {
                    cmbRoomLst = "\"" + str + "\"";
                }
            }
        }
    }
}
